package com.zyf.chapter02.behavior.chain.framework.servlet;

import java.util.ArrayList;
import java.util.List;

/**
 * 组合类 模拟 ApplicationFilterChain 的实现方式
 * @author yifeng
 */
public class HandlerChain {
    private List<IHandler> handlers = new ArrayList<>();
    int n = 0;

    public void addHandler(IHandler handler) {
        this.handlers.add(handler);
    }

    public void handle() {
        if (n < handlers.size()) {
            IHandler handler = handlers.get(n++);
            handler.handler(this);
        }
        // 职责链调用完毕
    }
}
